<?php
include_once('IXR_Library.php'); 
$usr = 'admin';
$pwd = 'kRo0PFkgNZT7zoUK';
$xmlrpc = 'http://runewyear.com/xmlrpc.php';
$client = new IXR_Client($xmlrpc);
//$client->debug = true;
function debug($var) {
	echo "<pre>";
	print_r($var);
	echo "</pre>";
}
function toAscii($str, $replace=array(), $delimiter='-') {
	setlocale(LC_ALL, 'en_US.UTF8');
	if( !empty($replace) ) {
		$str = str_replace((array)$replace, ' ', $str);
	}

	$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
	$clean = strtolower(trim($clean, '-'));
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

	return $clean;
}
if(isset($_POST['keyword']) && isset($_POST['title'])) {
	$keyword = urlencode($_POST['keyword']);
	$jsonurl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&imgsz=large&as_filetype=jpg&q={$keyword}";
	$json = file_get_contents($jsonurl,0,null,null);
	
	$jsonurl1 = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=7&start=8&imgsz=large&as_filetype=jpg&q={$keyword}";
	$json1 = file_get_contents($jsonurl1,0,null,null);
	
	$json_output1 = json_decode($json);
	$json_output2 = json_decode($json1);
	$imgs = array();
	
	foreach($json_output1->responseData->results as $img) {
		$imgurl = $img->url;
		$imgtitle = $img->titleNoFormatting;
		$imgcontent = $img->contentNoFormatting;	
		$tmp = array(
			'imgurl' => $imgurl,
			'imgtitle' => $imgtitle,
			'imgcontent' => $imgcontent
		);
		$imgs[] = $tmp;
		unset($tmp);
	}
	
	foreach($json_output2->responseData->results as $img) {
		$imgurl = $img->url;
		$imgtitle = $img->titleNoFormatting;
		$imgcontent = $img->contentNoFormatting;	
		$tmp = array(
			'imgurl' => $imgurl,
			'imgtitle' => $imgtitle,
			'imgcontent' => $imgcontent
		);
		$imgs[] = $tmp;
		unset($tmp);
	}
	
	foreach($imgs as $key => $img) {
		$img_name = $img['imgtitle'];
		$img_name = toAscii($img_name).'.jpg';
		
		$fh = @file_get_contents($img['imgurl'], 'r');
		if($fh != false) {
			$params = array('name' => $img_name, 'type' => 'image/jpg', 'bits' => new IXR_Base64($fh), 'overwrite' => false);
			if($client->query('wp.uploadFile',1, $usr, $pwd, $params)) {
				$resp = $client->getResponse();
				$imgs[$key]['imgurl'] = $resp['url'];
			} else {
				unset($imgs[$key]);
			}
		} else {
			unset($imgs[$key]);
		}
		unset($fh);		
	}
	
	$post = array();
	$post['post_type'] = 'post';
	$post['post_title'] = $_POST['title'];
	$post['post_content'] = "";
	//$post['mt_keywords'] = "tag keyword 1, tag keyword 2, tag keyword 3"; // post tags
	$post['mt_allow_comments'] = 0; // allow comments
	$post['mt_allow_pings'] = 1; // allow pings
	$post['post_status'] = 'publish';
	$post['terms_names'] = array('post_tag' => array($_POST['keyword'], 'funny russian new years pictures', 'happy russian new year 2013', 'russian new year 2013 funny photos'));
	//$post['post_date'] = date('Y-m-d H:i:s');
	foreach($imgs as $img) {
		$post['post_content'] .= "<div class='img-item'>";
		$post['post_content'] .= "<img width='584px' title='" . $img['imgtitle'] . " " . $img['imgcontent'] . " " . $_POST['title'] . "' alt='" . $img['imgcontent'] . " " . $img['imgtitle'] . " " . $_POST['title'] . "' src='" . $img['imgurl'] . "' />";
		$post['post_content'] .= "</div>";
		$post['post_content'] .= "<div class='img-caption'>";
		$post['post_content'] .= "<p class='caption'>" . $img['imgtitle'] . " " . $img['imgcontent'] . " " . $_POST['title'] . "</p>";
		$post['post_content'] .= "</div>";
	}
	
	// Create a new post
    if (!$client->query('wp.newPost',1, $usr,$pwd, $post)) {
        die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
 
    print $client->getResponse()."\n";
}
?>
<h2> Russian New Year Fun Photos </h2>
<form name="test" method="post" accept-charset="utf-8">
<table>
	<tr>
		<td align="right">Keyword:</td>
		<td><input type="text" name="keyword" size="64" value="" /></td>
	</tr>
	<tr>
		<td align="right">Title:</td>
		<td><input type="text" name="title" size="64" value="" /></td>
	</tr>
</table>
<input type="submit" name="ok" value="Go" />
</form>